sources:=$(wildcard src/*.cpp  src/*.c)
Fsources:=$(wildcard  src/*.F src/*.f90)
incs:=$(wildcard src/*.hpp inc/*.hpp)
obj1 := $(patsubst src/%, %, $(sources))
obj1:=$(obj1:.cpp=.o)
obj1:=$(obj1:.cu=.o)
obj1:=$(obj1:.c=.o)

obj2 := $(patsubst src/%, %, $(Fsources))
obj2:=$(obj2:.F=.o)
obj2:=$(obj2:.f90=.o)
obj2:=$(obj2:module_halo.o=)
obj2:=$(obj2:module_gcr.o=)
obj2:=$(obj2:h1.o=)
obj2:=$(obj2:tmp.o=)
obj:=h1.o module_halo.o  $(obj2) $(obj1)
OBJS := $(addprefix obj/, $(obj))
SRCDIR = src
BUILDDIR = obj


CC :=mpicc
FC :=mpif90
CFLAGS = -O3   -auto-scalar 
FFLAGS = -FR -O3 -fp-model strict -mcmodel=medium -zero -auto-scalar -I/opt/hpc/software/mpi/hpcx/v2.7.4/intel-2017.5.239/include -L/opt/hpc/software/mpi/hpcx/v2.7.4/intel-2017.5.239/lib #-fopenmp 
ADDLDFLAGS =  -lstdc++ -L ../../lib -lamdhip64 -L /public/software/compiler/dtk/dtk-21.10/lib  -L -L/public/software/compiler/dtk/dtk-21.10/hipblas/lib -lhipblas -L /public/software/mathlib/netcdf/4.4.1/intel/lib -lnetcdf -lmkl_core -lmkl_intel_lp64 -lmkl_sequential -L /public/software/compiler/dtk/dtk-21.10/hipsparse/lib -lhipsparse
INC=-I /public/software/compiler/dtk/dtk-21.10/hipsparse/include -I/public/software/mathlib/netcdf/4.4.1/intel/include/ -I ~/usr/gptl/include/ -march=core-avx2   -I inc #-I inc_spmv #-fopenmp  
#-Wno-extern-initializer 

main:$(OBJS) 
	$(FC) $(FFLAGS) $(OBJS)  -o run.exe  -lstdc++  -lgptl -L ~/usr/gptl/lib/  -DUSE_MKL   -DICC -qmkl  $(ADDLDFLAGS) -fopenmp 

$(BUILDDIR)/%.o: $(SRCDIR)/%.c $(incs)
	mpicc -c -O3  $<  -o $@  $(INC) -fopenmp 

$(BUILDDIR)/psolve_gcr.o: $(SRCDIR)/psolve_gcr.cpp $(incs)
	mpicc -c -O3  $<  -o $@  $(INC) -fopenmp 

$(BUILDDIR)/%.o: $(SRCDIR)/%.cpp $(incs)
	hipcc -c -O3 -std=c++11  $<  -o $@  $(INC)
	

$(BUILDDIR)/%.o: $(SRCDIR)/%.f90
	$(FC) $(FFLAGS)  -c $<  -I./ -o $@  -I ~/usr/gptl/include/ 
$(BUILDDIR)/%.o: $(SRCDIR)/%.F
	icc -E $< > tmp.f90
	$(FC) $(FFLAGS)  -c tmp.f90  -I./ -o $@  -I ~/usr/gptl/include/ 
	rm tmp.f90

run:
	$(shell [ -e outfile_cu ] &&  mv outfile_cu runlog/outfile_cu.$(shell date +""%m-%d-%H%M""))
	$(shell [ -e outfile_ca ] &&  mv outfile_cu runlog/outfile_ca.$(shell date +""%m-%d-%H%M""))
	$(shell [ -e outfile_f90 ] &&  mv outfile_f90 runlog/outfile_f90.$(shell date +""%m-%d-%H%M""))
	$(shell [ -e outfile ] && mv outfile runlog/outfile.$(shell date +""%m-%d-%H%M""))
	sbatch  run.slurm
	#time mpirun -np 16  ./run.exe |tee runlog/$(shell date +""%m-%d-%H%M"").log 
	#mpirun -np 16 ./run.exe

rmlog:
	rm $(shell ls runlog/*.out -tr | tail -n 1)
clean:
	rm -f run.exe obj/* *.mod
clear:
	ps -ef|grep run.exe|grep -v grep|awk  '{print "kill -9 " $2}' |sh

#print:
#    $(info obj is $(obj))
#    $(info OBJS is $(OBJS))
#nvcc -O3 -c $< -o $@  -gencode=arch=compute_70,code=sm_70 
